<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { userApi } from '../api/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const currentUser = ref(null)

onMounted(() => {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    currentUser.value = JSON.parse(userStr)
  }
})

const handleLogout = async () => {
  try {
    const refreshToken = localStorage.getItem('refresh_token')
    await userApi.logout({ refresh_token: refreshToken })
    localStorage.clear()
    ElMessage.success('退出成功')
    await router.push('/login')
  } catch (error) {
    ElMessage.error('退出失败')
  }
}

const user = computed(() => {
  const userStr = localStorage.getItem('user')
  return userStr ? JSON.parse(userStr) : null
})

const isAdmin = computed(() => {
  return user.value && (user.value.is_admin || user.value.is_superuser)
})

const goToFrontend = () => {
  router.push('/frontend')
}

const goToBackend = () => {
  if (user.value && (user.value.is_admin || user.value.is_superuser)) {
    router.push('/backend')
  } else {
    ElMessage.error('您没有访问后台的权限')
  }
}
</script>

<template>
  <div class="home-container">
    <div class="overlay">
      <div class="logout-container">
        <el-button type="danger" @click="handleLogout">退出登录</el-button>
      </div>
      <div class="content">
        <h1>猫眼电影数据分析系统</h1>
        <p class="subtitle">发现数据背后的电影故事</p>
        <div class="button-group">
          <el-button 
            type="primary" 
            size="large"
            class="entrance-button"
            @click="goToFrontend"
          >
            进入前台
          </el-button>
          <el-button 
            type="warning" 
            size="large"
            class="entrance-button"
            @click="goToBackend"
            :disabled="!isAdmin"
          >
            进入后台
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
:root {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.5;
  font-weight: 400;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* 响应式基础样式 */
html, body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
}

#app {
  width: 100%;
  min-height: 100vh;
}

/* 响应式容器 */
.container {
  width: var(--mobile-width);
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 1rem;
}

/* 响应式文本 */
h1 {
  font-size: clamp(1.5rem, 5vw, 2.5rem);
  line-height: 1.2;
}

h2 {
  font-size: clamp(1.25rem, 4vw, 2rem);
  line-height: 1.3;
}

p {
  font-size: clamp(0.875rem, 3vw, 1rem);
  line-height: 1.6;
}

/* 响应式按钮 */
button {
  padding: clamp(0.5rem, 2vw, 1rem) clamp(1rem, 4vw, 2rem);
  font-size: clamp(0.875rem, 3vw, 1rem);
}

/* 响应式表单 */
input, select, textarea {
  width: 100%;
  padding: clamp(0.5rem, 2vw, 0.75rem);
  font-size: clamp(0.875rem, 3vw, 1rem);
}

/* 响应式间距 */
.spacing {
  margin: clamp(1rem, 4vw, 2rem) 0;
}

/* 响应式网格 */
.grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
}

/* 暗色主题 */
@media (prefers-color-scheme: dark) {
  :root {
    color: rgba(255, 255, 255, 0.87);
    background-color: #242424;
  }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .container {
    padding: 0.5rem;
  }
  
  .spacing {
    margin: 0.75rem 0;
  }
  
  .grid {
    gap: 0.75rem;
  }
}

/* Element Plus 组件的全局响应式样式 */
:deep(.el-button) {
  padding: 0.5rem 1rem;
  font-size: 1rem;
}

:deep(.el-input__wrapper) {
  padding: 0.5rem;
}

:deep(.el-form-item) {
  margin-bottom: 1rem;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  :deep(.el-button) {
    padding: 0.4rem 0.8rem;
    font-size: 0.9rem;
  }
  
  :deep(.el-input__wrapper) {
    padding: 0.4rem;
  }
  
  :deep(.el-form-item) {
    margin-bottom: 0.8rem;
  }
}
</style>

<style scoped>
.home-container {
  width: 100%;
  height: 100vh;
  min-height: 100vh;
  background: url('@/assets/background.jpg') no-repeat center center;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.content {
  text-align: center;
  color: #fff;
  z-index: 2;
}

.logout-container {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 3;
}

h1 {
  font-size: clamp(2rem, 6vw, 4rem);
  margin-bottom: clamp(20px, 4vw, 40px);
  color: #fff;
}

.subtitle {
  font-size: clamp(1rem, 3vw, 1.5rem);
  color: #fff;
  opacity: 0.9;
  margin-bottom: 40px;
}

.button-group {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 30px;
}

.entrance-button {
  min-width: 150px;
  font-size: clamp(1rem, 2vw, 1.2rem);
  padding: 15px 30px;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .logout-container {
    top: 10px;
    right: 10px;
  }
  
  .content {
    padding: 0 20px;
  }

  .button-group {
    flex-direction: column;
    gap: 15px;
  }

  .entrance-button {
    width: 100%;
    padding: 12px 20px;
  }
}
</style> 